Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hazae41/bytes

Package Overview
Dependencies
Maintainers
0
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hazae41/bytes

Utilities to deal with Uint8Array

  • 1.2.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Bytes

Utilities to deal with sized Uint8Array

npm i @hazae41/bytes

Node Package 📦

Features

  • 100% TypeScript and ESM
  • Rust-like patterns
  • Strongly typed Uint8Array and ArrayLike size
  • Zero-cost abstraction over Uint8Array and ArrayLike
  • Zero-copy conversion from ArrayBufferView
  • Use native Buffer for faster execution on Node
  • Unit-tested

Usage

Sized bytes
const bytes8 = Bytes.alloc(8) // Bytes<8>
Unsafe-allocated sized bytes
const bytes8 = Bytes.allocUnsafe(8) // Bytes<8>
Random sized bytes
const bytes8 = Bytes.random(8) // Bytes<8>
Unknown-sized bytes
const bytesX = new Uint8Array(8) // Bytes<number>
Runtime type-guarding
if (Bytes.is(bytesX, 8))
  bytesX // Bytes<8>
else
  bytesX // Bytes<number>
Type-guarded runtime equality check
if (Bytes.equals(bytesX, bytes8))
  bytesX // Bytes<8>
else
  bytesX // Bytes<number>
Runtime casting with Result pattern
const bytes16 = Bytes.tryCast(bytesX, 16).unwrap() // Bytes<16>
Conversion from sized arrays
const sized4 = Sized.cast([1, 2, 3, 4], 4).unwrap() // Sized<4>
const bytes4 = Bytes.from(sized4) // Bytes<4>
Utf8, Hex, Base64, Ascii encoding
Bytes.fromUtf8(Bytes.toUtf8(bytesX))
Bytes.fromHex(Bytes.toHex(bytesX))
Bytes.fromBase64(Bytes.toBase64(bytesX))
Bytes.fromAscii(Bytes.toAscii(bytesX))
BigInt conversion
Bytes.fromBigInt(Bytes.toBigInt(bytesX))
Sized slicing and padding
const bytes8 = Bytes.sliceOrPadStart(bytesX, 8) // Bytes<8>

Keywords

FAQs

Package last updated on 31 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc